﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using sqengine.Components;
#endregion


namespace sqengine.PhysicObjects
{
    public class ItmTerminal : MovableObject
    {
        private int browse_id = -1;
        private bool mouse_ent = false;
        private InputBox in_box;

        public ItmTerminal(Game game, string ObjName, Model model, Matrix orientation, float ObjScale, Vector3 position)
            : base(game, ObjName, model, orientation, ObjScale, position, true)
        {
            in_box = new InputBox(game, Adcontent, ((sqengine)game).Keyman, "Fonts/Visitor");

            CurrentGame.Components.Add(in_box);
            in_box.DrawOrder = 1;

            ActionDistance = 10.0f;
            in_box.OnClose += new InputBox.InputHandler(in_box_OnClose);
            OnAction += new ActionHandler(ItmTerminal_OnAction);
            OnMouseOver += new MouseEventHandler(ItmTerminal_OnMouseOver);
            OnMouseLeave += new MouseEventHandler(ItmTerminal_OnMouseLeave);
        }

        void in_box_OnClose(object sender, InputBox.InputArg ca)
        {
            CurrentWebBrowserPool.Navigate(browse_id, ca.Input());
        }

        void ItmTerminal_OnMouseLeave(object send, MouseArg e)
        {
            mouse_ent = false;
            if (ActionDistance > e.Distance())
                if (in_box.IsVisible)
                    in_box.Close();
        }

        void ItmTerminal_OnMouseOver(object send, MouseArg e)
        {
            mouse_ent = true;
        }

        void ItmTerminal_OnAction(object sender, ActionArg ca)
        {
            if (browse_id == -1)
            {
                browse_id = CurrentWebBrowserPool.AddWebWindow(640, 480);
                CurrentWebBrowserPool.Navigate(browse_id, "http://www.youtube.com/");
            }
            else
            {
                if (!in_box.IsVisible)
                    in_box.Show(-1, -1);

            }
        }

        public override void ApplyEffects(BasicEffect effect)
        {
            if (mouse_ent)
                effect.Alpha = 1.0f;
            else
                effect.Alpha = 0.4f;
        }

        public override void Update(GameTime gameTime)
        {
            if (browse_id != -1)
                ObjectTexture = CurrentWebBrowserPool.GetRendererWebpage(browse_id);

            base.Update(gameTime);
        }
    }
}
